using System.Collections;
using System.Xml;

public static class XMLParserTools
{
	public static bool TryGetAttribute(XmlNode node, string key, out object output)
	{
		output = null;
		if( node == null) return false;
		XmlNode item = node.Attributes.GetNamedItem(key);
		if( item == null)
			return false;
		
		output = item.Value;
		
		return true;
	}

	public static string GetAttribute(XmlNode node, string key)
	{
		return GetAttribute(node, key, "");
	}

	public static string GetAttribute(XmlNode node, string key, string defValue)
	{
		if (node == null) return defValue;
		if (node.Attributes.GetNamedItem(key) != null)
			return node.Attributes.GetNamedItem(key).Value;
		return defValue;
	}

	public static int GetIntAttribute(XmlNode node, string key)
	{
		return GetIntAttribute(node, key, 0);
	}

	public static int GetIntAttribute(XmlNode node, string key, int defValue)
	{
		if (node == null) return defValue;
		if (node.Attributes.GetNamedItem(key) != null)
			return int.Parse(GetAttribute(node, key));
		return defValue;
	}

	public static float GetFloatAttribute(XmlNode node, string key)
	{
		return GetFloatAttribute(node, key, 0);
	}

	public static float GetFloatAttribute(XmlNode node, string key, float defValue)
	{
		if (node == null) return defValue;
		if (node.Attributes.GetNamedItem(key) != null)
			return float.Parse(GetAttribute(node, key));
		return defValue;
	}

	public static bool GetBoolAttribute(XmlNode node, string key)
	{
		return GetBoolAttribute(node, key, false);
	}

	public static bool GetBoolAttribute(XmlNode node, string key, bool defValue)
	{
		if (node == null) return defValue;
		if (node.Attributes.GetNamedItem(key) != null)
			return bool.Parse(GetAttribute(node, key));
		return defValue;
	}
}
